package com.example.front.serivce;

import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.common.exception.BusinessException;
import com.example.front.dto.BoxCreateDto;
import com.example.front.dto.BoxDto;
import com.example.front.dto.BoxSearchDto;
import com.example.front.dto.DashboardUpdataDto;
import com.example.front.entity.*;
import com.example.front.mapper.BoxDashboardMapper;
import com.example.front.mapper.BoxMapper;
import com.example.passport.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BoxDashboardService {
    @Resource
    BoxDashboardMapper boxDashboardMapper;
    @Resource
    BoxService boxService;


    /**
     * 列出盒子看板
     */
    public List<BoxDto> list(Dashboard condition) {
        List<BoxDashboard> boxDashboards = boxDashboardMapper.find(condition);
        List<BoxDto> boxDtos = new ArrayList<>();
        for (BoxDashboard boxDashboard : boxDashboards){
            Box box = boxService.findById(boxDashboard.getTenantId(),boxDashboard.getBoxId());
            if(box!=null){
                BoxDto boxDto = new BoxDto();
                BeanUtils.copyProperties(box,boxDto);
                boxDtos.add(boxDto);
            }
        }
        return boxDtos;
    }

    /**
     * 创建盒子看板
     */
    public void create(@Valid BoxCreateDto boxCreateDto) {

    }

    /**
     * 修改盒子看板
     */
    @Transactional
    public void update(DashboardUpdataDto dashboardUpdataDto) {
//        全部删除
        boxDashboardMapper.deleteByDashboard(dashboardUpdataDto.getTenantId(), dashboardUpdataDto.getId());
        for (BoxDto boxDto : dashboardUpdataDto.getBoxList()) {
            boxDashboardMapper.create(dashboardUpdataDto.getTenantId(), boxDto.getId(), dashboardUpdataDto.getId());
        }
    }

    /**
     * j
     * 删除盒子看板
     */
    public void deleteById(long id, long tenantId) {
        boxDashboardMapper.deleteByDashboard(tenantId,id);
    }

    /**
     * 搜索盒子
     */
//    public Box findByGatewayAddr(long tenantId , long id ,int addr) {
//
//    }

    /**
     * 搜索盒子
     */
//    public Box findById(long tenantId , long id ) {
//
//    }
}
